const { create } = require("../server/fileServer")
const { updateAvatarUrlToUsersServer } = require("../server/userServer")
const { SERVER_HOST , SERVER_PORT } = require('../config/server')


class FileControll {
    async create(ctx,next){
        const { filename , mimetype , size } = ctx.request.file
        const {id} = ctx.user
        // 创建avatar表的数据
        const result = await create(filename , mimetype , size , id)

        // 更新users表的avatar地址
		const avatar_url = `${SERVER_HOST}:${SERVER_PORT}/users/avatar/${id}`

        const result2 = await updateAvatarUrlToUsersServer(avatar_url , id)
 
        if(result && result2){
            ctx.body = {
                code:0,
                message:"头像上传成功~",
                result
            }
        }
    }
}

module.exports = new FileControll()